package com.stbu;

import com.stbu.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Controller
@SessionAttributes(names = {"regDate"},types = {User.class})
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/toReg")
    public String toReg(){

        return "reg";
    }
    @RequestMapping("/reg")
    public String reg(@RequestParam("username") String username,
                      @RequestParam("password")String password, Model model){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        Date regDate = new Date();
        model.addAttribute("regDate",regDate);
        model.addAttribute("user",user);
        return "forward:toLogin";
    }
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "Login";
    }
    @RequestMapping("/Login")
    public ModelAndView Login(@RequestParam("username") String uLogin,
                              @RequestParam("password")String pLogin,
                              @SessionAttribute("user") User regUser,ModelAndView mView){
        String msg="";
        if(uLogin.equalsIgnoreCase(regUser.getUsername()) && pLogin.equalsIgnoreCase(regUser.getPassword())){
            msg = "登录成功";
        }else {
            msg = "登录失败";
        }
        mView.addObject("username",uLogin);
        mView.addObject("msg",msg);
        mView.setViewName("result");
        return mView;
    }
}

